// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function SlimeWander()
{
	sprite_index = sprMove;
	//抵达目的地或者放弃前往目的地
	if((x == xTo) && (y == yTo)||(timePassed > enemyWanderDistance / enemySpeed))
	{
		hSpeed = 0;
		vSpeed = 0;
		
		//停止精灵动画
		if(image_index < 1)
		{
			image_speed = 0.0;
			image_index = 0;
		}
		//设置新的目的地
		if(++wait >= waitDuration)
		{
			wait = 0;
			timePassed = 0;
			dir = point_direction(x, y, xstart, ystart) + irandom_range(-45, 45);
			xTo = x + lengthdir_x(enemyWanderDistance, dir);
			yTo = y + lengthdir_y(enemyWanderDistance, dir);
		}
		
	}
	else//向新的目的地移动
	{
		timePassed++;
		image_speed = 1.0;
		var _distanceToGo = point_distance(x, y, xTo, yTo);
		var _speedThisFrame = enemySpeed;
		if(_distanceToGo <enemySpeed) _speedThisFrame = _distanceToGo;
		dir = point_direction(x, y, xTo, yTo);
		hSpeed = lengthdir_x(_speedThisFrame, dir);
		vSpeed = lengthdir_y(_speedThisFrame, dir);
		if(hSpeed != 0)image_xscale = sign(hSpeed);
		
		//碰撞和移动
		EnemyTileCollision();
	}
	//愤怒检测
	if(++aggroCheck >= aggroCheckDuration)
	{
		aggroCheck = 0;
		if(instance_exists(oPlayer)&&point_distance(x, y, oPlayer.x, oPlayer.y) < 50)
		{
			state = ENEMYSTATE.CHASE;
			target = oPlayer;
		}
	}
	
	
}

function SlimeChase()
{
	sprite_index = sprMove;
	
	if(instance_exists(target))
	{
		xTo = target.x;	
		yTo = target.y;
		
		var _distanceToGo = point_distance(x, y, xTo, yTo);
		image_speed = 1.0;
		dir = point_direction(x, y, xTo, yTo);
		if(_distanceToGo > enemySpeed)
		{
			hSpeed = lengthdir_x(enemySpeed, dir);
			vSpeed = lengthdir_y(enemySpeed, dir);
		}
		else
		{
			hSpeed = lengthdir_x(_distanceToGo, dir);
			vSpeed = lengthdir_y(_distanceToGo, dir);	
		}
		if (hSpeed != 0) image_xscale = sign(hSpeed);
		//碰撞与移动
		EnemyTileCollision();
	}
	//攻击检测
	if(instance_exists(target) && (point_distance(x, y, target.x, target.y) <= enemyAttackRadius))
	{
		state = ENEMYSTATE.ATTACK;
		sprite_index = sprAttack;
		image_index = 0;
		image_speed = 1.0;
		//攻击目标为player身后8像素
		xTo += lengthdir_x(8,dir);
		yTo += lengthdir_y(8,dir);
		
	}
	
}

function SlimeAttack()
{
	var _spd = enemySpeed;
	//当准备起跳时停止移动
	if(image_index < 2) _spd = 0;
	//当在空中或落地时
	if((floor(image_index) == 3) ||(floor(image_index == 5))) image_speed = 0;
	
	//跳跃距离
	var _distanceToGo = point_distance(x, y, xTo, yTo);
	//快要降落时
	if(_distanceToGo < 4)&&(image_index < 5) image_speed = 1;
	
	//移动
	if(_distanceToGo > _spd)
	{
		dir = point_direction(x, y, xTo, yTo);
		hSpeed = lengthdir_x(_spd, dir);
		vSpeed = lengthdir_y(_spd, dir);
		if(hSpeed != 0) image_xscale = sign(hSpeed);
		
		//判定移动并在撞墙时停止
		if(EnemyTileCollision())
		{
			xTo = x;
			yTo = y;
		}
	}
	else
	{
		x = xTo;
		y = yTo;
		if(floor(image_index) == 5)
		{
			stateTarget = ENEMYSTATE.CHASE;
			stateWaitDuration = 15;
			state = ENEMYSTATE.WAIT;
		}
	}
}

function SlimeHurt()
{
	sprite_index = sprHurt;
	var _distanceToGo = point_distance(x, y, xTo, yTo);
	if(_distanceToGo > enemySpeed)
	{
		image_speed = 1.0;
		dir = point_direction(x, y, xTo, yTo);
		hSpeed = lengthdir_x(enemySpeed, dir);
		vSpeed = lengthdir_y(enemySpeed, dir);
		if(hSpeed != 0) image_xscale = -sign(hSpeed);
		
		//碰撞和移动，如果有碰撞，停止击退
		if(EnemyTileCollision())
		{
			xTo = x;
			yTo = y;
		}
	}
	else
	{
		x = xTo;
		y = yTo;
		if(statePrevious != ENEMYSTATE.ATTACK)state = statePrevious;else state = ENEMYSTATE.CHASE;
	}
}

function SlimeDie()
{
	sprite_index = sprDie;
	image_speed = 1.0;
	var _distanceToGo = point_distance(x, y, xTo, yTo);
	if(_distanceToGo > enemySpeed)
	{
		dir = point_direction(x, y, xTo, yTo);
		hSpeed = lengthdir_x(enemySpeed,dir);
		vSpeed = lengthdir_y(enemySpeed,dir);
		if(hSpeed != 0)image_xscale = -sign(hSpeed);
		//碰撞和移动
		EnemyTileCollision();
	}
	else
	{
		x = xTo;
		y = yTo;
	}
	if( image_index + (sprite_get_speed(sprite_index)/game_get_speed(gamespeed_fps)) >= image_number)
	{
		instance_destroy();
	}

	
	
}
